//
//  TJVisitorView.m
//  TJSinaWeiBo_OC
//
//  Created by 唐后能 on 16/4/13.
//  Copyright © 2016年 itheima. All rights reserved.
//

#import "TJVisitorView.h"

#import "Masonry.h"

@interface TJVisitorView ()

@property (nonatomic, strong) UIImageView *wheelImgView; //轮子.

@property (nonatomic, strong) UIImageView *houseImgView; //房子.

@property (nonatomic, strong) UILabel *messageLabel; //文本框.

@property (nonatomic, strong) UIImageView *coverImgView; //遮盖.

@end
@implementation TJVisitorView

-(instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {

    if (self = [super initWithCoder:aDecoder]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    self.backgroundColor = TJSameColor(237);
    [self wheelRotation];
    // 1,添加子控件.
    // 1.1,转轮.
    UIImageView* wheelImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
    [self addSubview:wheelImgView];
    self.wheelImgView = wheelImgView;
    
    // 添加遮罩.
    UIImageView *coverImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    [self addSubview:coverImgView];
    self.coverImgView = coverImgView;
    
    // 1.2,房子.
    UIImageView* houseImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
    [self addSubview:houseImgView];
    self.houseImgView = houseImgView;
    
    // 1.3,mesageLabel.
    UILabel* messageLabel = [[UILabel alloc] init];
    messageLabel.text = @"关注一些人,看看有神马惊喜!";
    messageLabel.textAlignment = NSTextAlignmentCenter;
    messageLabel.textColor = [UIColor blackColor];
    messageLabel.font = [UIFont systemFontOfSize:16];
    messageLabel.numberOfLines = 0;
    [self addSubview:messageLabel];
    self.messageLabel = messageLabel;
    
    // 1.4,注册按钮.
    UIButton * registerBtn = [[UIButton alloc] init];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [registerBtn setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:registerBtn];
    
    // 给注册按钮添加点击事件.
    [registerBtn addTarget:self action:@selector(registBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    
    // 1.5,登录按钮.
    UIButton* loginBtn = [[UIButton alloc] init];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [loginBtn setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:loginBtn];
    
    // 给登录按钮添加点击事件.
    [loginBtn addTarget:self action:@selector(loginBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    
    // 2,添加约束.
    // 先去除AutoResizing.
    wheelImgView.translatesAutoresizingMaskIntoConstraints = NO;
    houseImgView.translatesAutoresizingMaskIntoConstraints = NO;
    messageLabel.translatesAutoresizingMaskIntoConstraints = NO;
    registerBtn.translatesAutoresizingMaskIntoConstraints = NO;
    loginBtn.translatesAutoresizingMaskIntoConstraints = NO;
    
    typeof(self)__weakself = self ;
    // 2.1,转轮.
    [wheelImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(__weakself);
        make.centerY.equalTo(__weakself).offset(-16);
    }];
    
    // 遮盖.
    [coverImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(__weakself);
        make.bottom.equalTo(registerBtn);
        
    }];
    // 2.2,房子.
    [houseImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(wheelImgView);
    }];
    
    // 2.3,消息框.
    [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(__weakself);
        make.top.equalTo(wheelImgView.mas_bottom).offset(16);
        make.width.mas_equalTo(250);
        make.height.mas_equalTo(40);
    }];
    
    // 2.4,注册按钮.
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(messageLabel.mas_bottom).offset(16);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(40);
        make.leading.equalTo(messageLabel);
    }];
    
    // 2.5,登录按钮.
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(registerBtn);
        make.width.equalTo(registerBtn);
        make.height.equalTo(registerBtn);
        make.trailing.equalTo(messageLabel);
    }];
}

#pragma mark - 显示不同的vistorVew.
- (void)showOtherVistorViewWithCenterImgName:(NSString *)imgName messageText:(NSString *)text {

    self.wheelImgView.hidden = YES;
    self.coverImgView.hidden = YES;
    self.houseImgView.image = [UIImage imageNamed:imgName];
    self.messageLabel.text = text;
}

#pragma mark - 转轮动画.
- (void) wheelRotation {

    [UIView animateWithDuration:3 delay:0 options:UIViewAnimationOptionCurveLinear  animations:^{
        self.wheelImgView.transform = CGAffineTransformRotate(self.wheelImgView.transform, M_PI);
    } completion:^(BOOL finished) {
        [self wheelRotation];
    }];
}

#pragma mark - 按钮点击事件回调方法.
- (void)registBtnClicked {
    
    if ([self.delegate respondsToSelector:@selector(registClicked)]) {
        [self.delegate registClicked];
    }
}

- (void)loginBtnClicked {

    if ([self.delegate respondsToSelector:@selector(loginClicked)]) {
        [self.delegate loginClicked];
    }
}

@end
